﻿using System;
using System.Collections.Generic;
using EngineDrawer.Effects.ParticleSystem;
using EngineDrawer.Effects.ParticleSystem.Core;
using Microsoft.Xna.Framework;
using ThinkDotNetEngine.Bots.Ammos;

namespace ThinkDotNetEngineDrawer.Bots.Ammos
{
    public class RocketParticleEffectsDrawer:AmmoDrawerBase
    {
        #region Fields

        private ProjectileTrailParticleSystem _projectileTrailParticleSystem;
        private ExplosionSystem _explosionSystem;
        private readonly Dictionary<AmmoBase, ParticleEmitter> _smokeTrailParticleEmitters = new Dictionary<AmmoBase, ParticleEmitter>();
        
        #endregion

        #region Consts

        private const short SmokeProjectileTrailParticlesPerSec = 60;

        #endregion

        public RocketParticleEffectsDrawer(Game game, Type supportableAmmoType) : base(game, supportableAmmoType)
        {
        }

        public override void Initialize()
        {
            _projectileTrailParticleSystem=new ProjectileTrailParticleSystem(Game,Game.Content);
            _explosionSystem=new ExplosionSystem(Game);
          
            Game.Components.Add(_projectileTrailParticleSystem);
            Game.Components.Add(_explosionSystem);
            
            base.Initialize();
        }
        public override void DrawEffects(GameTime gameTime, AmmoBase ammo)
        {
            _projectileTrailParticleSystem.SetCamera(World.ViewMatrix, World.ProjMatrix);           

            _smokeTrailParticleEmitters[ammo].Update(gameTime,ammo.CurrentPosition);
        }        

        #region Properties     

        #endregion

        #region Private\Remove Methods

        virtual protected void RemoveAmmo(AmmoBase ammoBase)
        {
            _smokeTrailParticleEmitters.Remove(ammoBase);
            _explosionSystem.Explode(ammoBase.CurrentPosition,ammoBase.CurrentDirection);
        }
   
        virtual protected void AddNewAmmo(AmmoBase ammoBase)
        {
            var particleEmitter = new ParticleEmitter(_projectileTrailParticleSystem,
                                                      SmokeProjectileTrailParticlesPerSec, ammoBase.CurrentPosition);
            _smokeTrailParticleEmitters.Add(ammoBase, particleEmitter);
        }

        #endregion

        #region Events

        protected override void OnAmmoAdded(AmmoBase ammoBase)
        {
            AddNewAmmo(ammoBase);
        }
        protected override void OnAmmoRemoved(AmmoBase ammoBase)
        {
            RemoveAmmo(ammoBase);
        }        

        #endregion
    }
}
